#include "sleep_api.h"
#include "common.h"
#include "formula.h"
#include "impl.h"
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

static uint64_t _cmp_hr_ts(const void *v) {
  smp_hr_t *smp = (smp_hr_t *)v;
  return smp->ts;
}

NEW_CMP(_cmp_smp, uint64_t, _cmp_hr_ts)

void v3_calc(smp_hr_t *hr_list, size_t len, sleep_root **root) {
  if (!hr_list || !len) {
    return;
  }
  // 按时间从小到大排序
  qsort(hr_list, len, sizeof(smp_hr_t), _cmp_smp);
  if (len < (size_t)(60 / get_smp_interval())) {
    if (root)
      *root = NULL;
    return;
  }

  sleep_impl(hr_list, len, root);
}
